<?php

/**
 * Description of ContactApiController
 *
 * @author Poh Nean
 */

class ContactApiController extends PsController
{	
	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'roles' => array('Contact.ManageAgency'),
					)), parent::accessRules());
	}
	
    /**
     * Updates a Cal Event. Handler for REST GET Method
     * Usage: GET api/contact
     */
    public function actionList($query = null)
    {
        $contacts = $this->loadContacts($query);

        $data = array();
        if (!empty($contacts)) {
            foreach ($contacts as $contact) {
                $temp = array();
                $temp["id"] = $contact->id;
                $temp["name"] = $contact->name;
                
                $data[] = $temp;
            }
        }

        // Send response
        $response = array();
        $response["success"] = true;
        $response["data"] = $data;

        print CJSON::encode($response);
    }


    public function getRequestVariables()
    {
        return json_decode(file_get_contents("php://input"));
    }
    
    public function loadContacts($query = null) {
        $criteria = new CDbCriteria;
        $criteria->condition = 'name LIKE :name AND t.deleted = 0 AND t.user_id = :user_id';
        $criteria->params = array(
            ':name' => $query . '%',
            ':user_id' => Yii::app()->user->id
        );
        $criteria->limit = 100;
        
        return Contact::model()->notRemoved()->findAll($criteria);
    }
}

?>
